package stu.day9_day18;

public class MyCircularDeque {

    int front = 0;
    int rear = 0;
    int[] array;

    int len = 0;

    public MyCircularDeque(int k) {
        array = new int[k+1];
        len = k+1;

    }

    public boolean insertFront(int value) {
        if(isFull()){
            return false;
        }else {
            front = (front-1+len)%len;
            array[front] = value;
            return true;
        }
    }

    public boolean insertLast(int value) {
        if(isFull()){
            return false;
        }else {
            array[rear] = value;
            rear = (rear+1)%len;
            return true;
        }
    }

    public boolean deleteFront() {
        if(isEmpty()){
            return false;
        }else {
            front = (front+1)%len;
            return true;
        }
    }

    public boolean deleteLast() {
        if(isEmpty()){
            return false;
        }else {
            rear = (rear-1+len)%len;
            return true;
        }
    }

    public int getFront() {
        if(isEmpty()){
            return -1;
        }else {
            return array[front];
        }
    }

    public int getRear() {
        if(isEmpty()){
            return -1;
        }else {
            return array[(rear-1+len)%len];
        }
    }

    public boolean isEmpty() {
        if(rear == front){
            return true;
        }else {
            return false;
        }

    }

    public boolean isFull() {
        if((rear+1)%len == front){
            return true;
        }else {
            return false;
        }

    }

}
